//
//  EWEUser.swift
//  MedicalUnionHybrid
//
//  Created by 来 on 2019/9/27.
//  Copyright © 2019 EWell. All rights reserved.
//

import UIKit

class EWEUser {
    
    public static let lgf: EWEUser = EWEUser()
    let userDefaults = UserDefaults.standard
    private init() { }
    
    var accessToken: String! {
        get {
            return autoGet("accessToken")
        }
        set {
            autoSet("accessToken", newValue)
        }
    }
    
    var avatar: String! {
        get {
            return autoGet("avatar")
        }
        set {
            autoSet("avatar", newValue)
        }
    }
    
    var menuList: String! {
        get {
            return autoGet("menuList")
        }
        set {
            autoSet("menuList", newValue)
        }
    }
    
    var loginName: String! {
        get {
            return autoGet("loginName")
        }
        set {
            autoSet("loginName", newValue)
        }
    }
    
    var hospitalId: String! {
        get {
            return autoGet("hospitalId")
        }
        set {
            autoSet("hospitalId", newValue)
        }
    }
    
    var hospitalName: String! {
        get {
            return autoGet("hospitalName")
        }
        set {
            autoSet("hospitalName", newValue)
        }
    }
    
    var abbreviation: String! {
        get {
            return autoGet("abbreviation")
        }
        set {
            autoSet("abbreviation", newValue)
        }
    }
    
    //
    var userId: String! {
        get {
            return autoGet("userId")
        }
        set {
            autoSet("userId", newValue)
        }
    }
    
    var wsxxuserId: String! {
        get {
            return autoGet("wsxxuserId")
        }
        set {
            autoSet("wsxxuserId", newValue)
        }
    }
    
    var realName: String! {
        get {
            return autoGet("realName")
        }
        set {
            autoSet("realName", newValue)
        }
    }
    
    var role: String! {
        get {
            return autoGet("role")
        }
        set {
            autoSet("role", newValue)
        }
    }
    
    var eduLevel: String! {
        get {
            return autoGet("eduLevel")
        }
        set {
            autoSet("eduLevel", newValue)
        }
    }
    
    var exam : String! {
        get {
            return autoGet("exam")
        }
        set {
            autoSet("exam", newValue)
        }

    }
    

    
    // 自动 get, userDefaults 第一次取的时候无值返回 ""
    func autoGet(_ key: String) -> String {
        if let value = userDefaults.object(forKey: key) as? String {
//            let decodeValue = EWERequest.aesDecode(value, "www.baidu.com", "www.baidu.vip")
            return value
        } else {
            return ""
        }
    }
    
    func autoSet(_ key: String, _ value: String!) {
//        let encodedValue = EWERequest.aesEncoded(value ?? "", "www.baidu.com", "www.baidu.vip")
        userDefaults.set(value, forKey: key)
    }
    
    class func logout() -> Void {
        EWEUser.lgf.accessToken = ""
        EWEUser.lgf.userId = ""
        EWEUser.lgf.wsxxuserId = ""
        EWEUser.lgf.avatar = ""
        EWEUser.lgf.loginName = ""
        EWEUser.lgf.role = ""
        EWEUser.lgf.accessToken = ""
        EWEUser.lgf.menuList = ""
    }

}
